<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的消息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        h1 {
            color: #333;
        }

        #messageList {
            list-style: none;
            padding: 0;
        }

        .message {
            border: 1px solid #ddd;
            margin-bottom: 10px;
            padding: 10px;
            background-color: #f9f9f9;
            border-radius: 5px;
            cursor: pointer;
        }

        .message.unread {
            background-color: #e0e0e0;
            font-weight: bold;
        }

        .timestamp {
            color: #888;
            font-size: 12px;
        }

        #sendMessageForm {
            margin-top: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input,
        textarea {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }

        button {
            background-color: #4caf50;
            color: white;
            padding: 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>

<h1>我的消息</h1>

<ul id="messageList"></ul>

<button id="readAllBtn">一键全部已读</button>

<form id="sendMessageForm">
    <label for="recipient">接收人：</label>
    <input type="text" id="recipient" required>
    <br>
    <label for="message">消息内容：</label>
    <textarea id="message" required></textarea>
    <br>
    <button type="button" id="sendMessageBtn">发送消息</button>
</form>

<script src="vendor/jquery.min.js" type="text/javascript"></script>

<script>
    $(document).ready(function () {
        // 消息列表数据
        var messages = [
            { id: 1, recipient: 'Alice Smith', content: 'Hello there!', timestamp: '2024-02-02 10:00 AM', read: false },
            { id: 2, recipient: 'John Doe', content: 'Hi! How are you?', timestamp: '2024-02-02 10:30 AM', read: true },
            // 添加更多消息
        ];

        // 初始化消息列表
        function initMessageList() {
            var $messageList = $('#messageList');
            $messageList.empty();

            messages.forEach(function (message) {
                var $messageItem = $('<li class="message">');
                $messageItem.attr('data-id', message.id);
                $messageItem.addClass(message.read ? 'read' : 'unread');
                $messageItem.append('<p>接收人: ' + message.recipient + '</p>');
                $messageItem.append('<p>' + message.content + '</p>');
                $messageItem.append('<p class="timestamp">' + message.timestamp + '</p>');

                $messageItem.click(function () {
                    showMessageDetail(message.id);
                });

                $messageList.append($messageItem);
            });
        }

        // 一键全部已读
        $('#readAllBtn').click(function () {
            messages.forEach(function (message) {
                message.read = true;
            });
            initMessageList();
        });

        // 发送消息
        $('#sendMessageBtn').click(function () {
            var recipient = $('#recipient').val();
            var messageContent = $('#message').val();
            var timestamp = new Date().toLocaleString();

            // 添加新消息
            var newMessage = {
                id: messages.length + 1,
                recipient: recipient,
                content: messageContent,
                timestamp: timestamp,
                read: false
            };

            messages.push(newMessage);

            // 更新消息列表
            initMessageList();

            // 清空输入框
            $('#recipient').val('');
            $('#message').val('');
        });

        // 显示消息详情
        function showMessageDetail(messageId) {
            var selectedMessage = messages.find(function (message) {
                return message.id === messageId;
            });

            if (selectedMessage) {
                alert('消息详情:\n\n' +
                    '接收人: ' + selectedMessage.recipient + '\n' +
                    '消息内容: ' + selectedMessage.content + '\n' +
                    '时间戳: ' + selectedMessage.timestamp);
            }
        }

        // 初始化消息列表
        initMessageList();
    });
</script>
</body>
</html>
